package com.web.portal.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.web.portal.mapper.LabelArticeMapper;
import com.web.portal.pojo.Article;
import com.web.portal.pojo.LabelArticle;
import com.web.portal.services.ArticleService;
import com.web.portal.services.LabelArticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LabelArticeServiceImpl extends ServiceImpl<LabelArticeMapper, LabelArticle>
        implements LabelArticeService {

    @Autowired
    private LabelArticeMapper labelArticeMapper;

    @Autowired
    private ArticleService articleService;

    @Override
    public List<Article> getListArticleByLabelCode(Long current, Long limit, String labelCode) {
        QueryWrapper<LabelArticle> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("label_code", labelCode).select("label_code", "content_code");
        queryWrapper.orderByDesc("id");
        Page<LabelArticle> labelArticlePage = labelArticeMapper.selectPage(new Page<>(current, limit), queryWrapper);
        ArrayList<Article> articles = new ArrayList<>();
        for (LabelArticle record : labelArticlePage.getRecords()) {
            String contentCode = record.getContentCode();
            articles.add(articleService.selectByArticleCode(contentCode));
        }
        return articles;
    }

    @Override
    public List<LabelArticle> selectByLabelCode(String labelCode) {
        QueryWrapper<LabelArticle> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("label_code", labelCode);
        return labelArticeMapper.selectList(queryWrapper);
    }
}
